package com.yx.changdao.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yx.changdao.common.annotation.AutoLog;
import com.yx.changdao.common.annotation.ShiroDesc;
import com.yx.changdao.common.data.RespCodeEnum;
import com.yx.changdao.common.data.Result;
import com.yx.changdao.common.data.ResultCodeEnum;
import com.yx.changdao.common.entity.CommunityTownship;
import com.yx.changdao.common.entity.CommunityVillage;
import com.yx.changdao.common.exception.AlertException;
import com.yx.changdao.common.utils.SysUtils;
import com.yx.changdao.common.vo.BuildBuildVo;
import com.yx.changdao.service.CommunityTownshipService;
import com.yx.changdao.service.CommunityVillageService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


/**
 * @Description: <br/>
 * 乡镇
 * <p>
 * <br/>
 * @Auther: walter
 * @create 2020/04/18 15:48
 */
@RestController
@RequestMapping("community/township")
public class CommunityTownshipController {

    /**
     * 乡镇服务
     */
    @Autowired
    private CommunityTownshipService communityTownshipService;
    @Autowired
    private CommunityVillageService communityVillageService;

    /**
     * 列表
     *
     * @return Result
     */

    /**
     * 总控
     */
    @AutoLog
    @GetMapping("/list")
//    @RequiresPermissions("community:township:list")
//    @ShiroDesc("乡镇:总控列表")
    /**
     * 这里直接去掉权限,因为前台的总控(5级)和执行(3级)都需要调用这个接口, 而一个接口只能有一个权限;
     */

    public Result list() throws AlertException {
        List<CommunityTownship> list = communityTownshipService.list(SysUtils.autoCnd(CommunityTownship.class));
        List<BuildBuildVo> buildBuildVoList=new ArrayList<>();

        for (CommunityTownship communityBuildDetail : list) {
            BuildBuildVo buildBuildVo=new BuildBuildVo();
            buildBuildVo.setId(communityBuildDetail.getId());
            buildBuildVo.setName(communityBuildDetail.getTownShipName());
            buildBuildVoList.add(buildBuildVo);
        }
        return Result.ok().data(buildBuildVoList);
    }



    /**
     * 添加乡镇
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PostMapping
    public Result add(@RequestBody CommunityTownship communityTownship) throws AlertException {
        CommunityTownship communityTownship1 = communityTownshipService.getOne(new QueryWrapper<CommunityTownship>()
                .eq(CommunityTownship.TOWN_SHIP_NAME, communityTownship.getTownShipName()));
        if(communityTownship1!=null){
            throw new AlertException("该乡镇已存在");
        }
        boolean b = communityTownshipService.save(communityTownship);

        if (!b) {
            throw new AlertException("添加失败");
        }
        CommunityTownship one = communityTownshipService.getOne(new QueryWrapper<CommunityTownship>()
                .eq(CommunityTownship.TOWN_SHIP_NAME, communityTownship.getTownShipName()));
        BuildBuildVo buildBuildVo =new BuildBuildVo();
        buildBuildVo.setId(one.getId());
        buildBuildVo.setName(one.getTownShipName());
        return Result.ok("添加成功").data(buildBuildVo);
    }

    /**
     * 获得指定id 乡镇。
     *
     * @param
     * @return Result
     */
    @AutoLog
    @GetMapping("/{id}")
    public Result get(@PathVariable("id") String id) throws AlertException {
        CommunityTownship communityTownship = communityTownshipService.getById(id);

        if (communityTownship == null) {
            throw new AlertException("未找到相关乡镇信息");
        }

        return Result.ok().data(communityTownship);
    }


    /**
     * 更新乡镇
     *
     * @param
     * @return Result
     */
    @AutoLog
    @PutMapping
    public Result update(@RequestBody CommunityTownship communityTownship) throws AlertException {
        CommunityTownship list = communityTownshipService.getOne(new QueryWrapper<CommunityTownship>().eq(CommunityTownship.TOWN_SHIP_NAME, communityTownship.getTownShipName()));
        if(list!=null&&!list.getId().equals(communityTownship.getId())){
            throw new AlertException(communityTownship.getTownShipName()+ " 乡镇已存在");
        }
        boolean b = communityTownshipService.updateById(communityTownship);
        if (!b) {
            throw new AlertException("更新失败");
        }

        return Result.ok("更新成功");
    }


    /**
     * 删除乡镇
     *
     * @param
     * @return Result
     */
    @AutoLog
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") String id) throws AlertException {
        CommunityTownship one = communityTownshipService.getOne(new QueryWrapper<CommunityTownship>().eq(CommunityTownship.ID, id));
        BuildBuildVo buildBuildVo =new BuildBuildVo();
        buildBuildVo.setId(one.getId());
        buildBuildVo.setName(one.getTownShipName());


//        boolean b1 = communityTownshipService.removeVillage(Integer.parseInt(id));
//        if(!b1){
//            throw new AlertException("删除失败");
//        }
        /**
         * 查看下级是否存在数据
         */
        List<CommunityVillage> list = communityVillageService.list(new QueryWrapper<CommunityVillage>()
                .eq(CommunityVillage.TOWN_SHIP_ID, id));
        if(list.size()!=0){
            return new Result(RespCodeEnum.OK, null, ResultCodeEnum.DELETE, "您删除的层级内包含其他数据信息!", buildBuildVo);
        }
        boolean b = communityTownshipService.removeById(id);
        if (!b) {
            throw new AlertException("删除失败");
        }

        return Result.ok("删除成功").data(buildBuildVo);
    }

}
